$(document).ready(function() {
	
	$('.styleswitch').click(function(){
		switchStylestyle(this.getAttribute("rel"));
		return false;
	});
	var c = readCookie('style');
	if (c) switchStylestyle(c);
});

//设置主题
function switchStylestyle(styleName){
	$('link[rel=stylesheet][title]').each(function(i){
		this.disabled = true;
		if (this.getAttribute('title') == styleName) 
			this.disabled = false;
	});
	
	$("iframe").contents().find('link[rel=stylesheet][title]').each(function(i) {
		this.disabled = true;
		if (this.getAttribute('title') == styleName) {
			this.disabled = false;
		}
	});
	createCookie('style', styleName, 365);
}
//cookie
function createCookie(name,value,days){
	if (days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,"",-1);
}
// /cookie functions




//退出
function logout(){
	$.messager.confirm('提示','您确认想要退出吗？',function(r){    
	    if (r){    
	    	window.location.href='j_spring_security_logout';
	    }    
	});
	
}

//创建tabs
function addTab(title, url, icon) {
	var content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:99%;"></iframe>';
	var t;
	if(window.parent) {
		t = window.parent.$('#index_tabs');
	} else {
		t = $('#index_tabs');
	}
	var opts = {
		title : title,
		closable : true,
		iconCls : icon,
		content : content,
		border : false,
		fit : true
	};
	if (t.tabs('exists', opts.title)) {
		t.tabs('select', opts.title);
		t.tabs('update',{
			tab: t.tabs('getSelected'),
			options: {
				content : content
			}
		});
	} else {
		var tabsCount = t.tabs('tabs').length; 
		if(tabsCount > 10) {
			$.messager.confirm("系统提示", '您当前打开了太多的页面，如果继续打开，会造成程序运行缓慢，无法流畅操作！', function (b) {                
				if (b) {
					t.tabs('add', opts);
				}
			});
		} else {
			t.tabs('add', opts);
		}
	}
}

//刷新tabs
function refreshTab(url) {
	var content = '<iframe src="' + url + '" frameborder="0" style="border:0;width:100%;height:98%;"></iframe>';
	return content;
}

//关闭tabs
function closeTab() {
	if(window.parent) {
		t = window.parent.$('#index_tabs');
		var tab = t.tabs('getSelected');
		var index = t.tabs('getTabIndex', tab);
		t.tabs('close', index);
	}
}


//form序列化json字符串
function form2Json(id) {
    var arr = $("#" + id).serializeArray();
    var jsonStr = "";
    jsonStr += '{';
    for (var i = 0; i < arr.length; i++) {
        jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",';
    }
    jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
    jsonStr += '}';
    /*      var json;
        if (typeof(JSON) == 'undefined'){  
           json = eval("("+jsonStr+")");  
        }else{  
            json = JSON.parse(jsonStr);  
        }  
    return json; */
    return jsonStr;
};
    

/**
 * 增加formatString功能
 * 
 * 使用方法：$.formatString('字符串{0}字符串{1}字符串','第一个变量','第二个变量');
 */
$.formatString = function(str) {
	for ( var i = 0; i < arguments.length - 1; i++) {
		str = str.replace("{" + i + "}", arguments[i + 1]);
	}
	return str;
};
    